<?php
/* ************************************************** *\
 * @company		: NovaITech							  *	
 * @author		: van.vo@novaitech.com				  * 
 * @copyright	: 2012								  * 
 * ================================================== *
 * @date		: {creation_date}
 * @project		: 
 * @description	: {description}
 *		- 
 *
\* ************************************************** */
class Categories extends MY_Controller {
    
    function __construct( ) {
        parent::__construct( );
		$this->load->library( array( 'form_validation', 'table' ) );		
		$this->load->helper( array( 'form', 'url' ) );
		$this->load->model( 'CodeGenerator_model', '', TRUE );
		$this->load_template();
	}	
	
	function index( ){
		$this->manage( );
	}

    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: View list
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		N/A
     |								
     | - Return			: 
     |			- N/A 
    \* ------------------------------------------------------------ */
    function view( ){
        // Get rules from config file
        // --------------------------------------------------
	    //$rules                  =   $this->form_validation->_config_rules[ 'categories_add' ];
        $rules                  =   array(array('field_id' => 'Ca_ID','label' => 'Ca_ID','control' => 'Array',),array('field_id' => 'Name','label' => 'Name','control' => 'Array',),array('field_id' => 'Create_Dt','label' => 'Create_Dt','control' => 'Array',),);
	    $this->data[ 'rules' ]  =   $rules;
       
        // Make header
        // --------------------------------------------------
	    $header = array( );
	    foreach( $rules as $item ){
	        $header[ ] = $item[ 'label' ];
	    }
       
	    $this->data[ 'header' ] =   $header;
	    $results                =   $this->CodeGenerator_model->get( 'categories', 'Ca_ID,Name,Create_Dt,Vip' );
	
        $actions  = array();
            
        
        
        // Make action Rent/Contact/Review
        // --------------------------------------------------
        for( $i = 0; $i < count( $results ); $i++ ){
            $id = array_values( $results[ $i ] );
            
            if(!empty($actions)){
                foreach ($actions as $item) {
                    $results[ $i ][ $item['action_name'] ]   = anchor( base_url( ) . 
                    'index.php/categories/'. $item['action_func'] . "/" . $id[ 0 ], $item['action_name']);
                }
            }
            
        }
        
        $this->data[ 'results'        ]   =   $results;
        $this->data[ 'master_limited' ]   =   explode(',', 'Ca_ID,Name,Create_Dt,Vip');
        $this->data[ 'custom_action'  ]   =   $actions;
 
        $this->load->view( 'categories_list', $this->data ); 
    }
    
    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Manage list
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		N/A
     |								
     | - Return			: 
     |			- N/A 
    \* ------------------------------------------------------------ */
    function manage( ){
		//paging
		$filter = $this->input->get('filter');
		$orderby = $this->input->get('orderby');
        $config['base_url'] = base_url().'categories/manage/';
		if($filter) {$config['suffix'] = "/?filter=$filter&orderby=$orderby";
			$config['first_url'] = $config['base_url']."0/?filter=$filter&orderby=$orderby";
		}

        $config['total_rows'] = $this->CodeGenerator_model->count("categories");
        $config['per_page'] = PER_PAGE;	
        $this->pagination->initialize($config); 
		$start = $this->uri->segment(3);
		if(empty($start)) $start = 0;
		$perpage = $config['per_page'];
		if(empty($filter)) $str_orderby = ''; else $str_orderby = "$filter $orderby";
        // Get rules from config file
        // --------------------------------------------------
        //$rules                  =   $this->form_validation->_config_rules[ 'categories_add' ];
        $rules                  =         array(
		array('field_id' => 'Name','label' => 'danh mục','control' => 'Array','width' => '80%', 'class' => 'first border-right', 'style' => 'border-right: solid 3px #fff;')
		);
        
       
        // Make header
        // --------------------------------------------------
        $header = array( );
        foreach( $rules as $k => $item ){
             $header[ ] = $item[ 'label' ];
			$str1='';$str2='';
			if($filter == $item['field_id']){
				if($orderby == "DESC") {$orderby = "ASC";$img1="display: none;";$img2="display: inline;"; } else {$orderby = "DESC";$img1="display: inline;";$img2="display: none;";}
				$url= base_url()."categories/manage/$start/?filter=$filter&orderby=$orderby";
				$str1 = 'onmouseout="$(\'.soimg2\').toggle()" onmouseover="$(\'.soimg2\').toggle()"';
				$str2='<img class="icon ic_s_asc soimg2" alt="Ascending" title="" src="'.base_url().'resources/images/dot.gif" style="'.$img1.'"> <img class="icon ic_s_desc soimg2 hide" alt="Descending" title="" src="'.base_url().'resources/images/dot.gif" style="'.$img2.'">';
			}else {
                            if($item['field_id']==="Vip"){
                                $url= base_url()."categories/manage/$start/?filter=".$item['field_id']."&orderby=DESC";
                            }else{
                                $url= base_url()."categories/manage/$start/?filter=".$item['field_id']."&orderby=ASC";
                            }
                        }
			$rules[$k]['html'] ='<span><a title="" '.$str1.' href="'.$url.'">'.$item[ 'label' ].' '.$str2.'</a></span>';
			if(isset($item['order'])&&!$item['order']) $rules[$k]['html'] = $item[ 'label' ];
        }
		
		$this->data[ 'rules' ]  =   $rules;
       
        $this->data[ 'header' ] =   $header;
        $results                =   $this->CodeGenerator_model->get( 'categories', 'Ca_ID,Name,Create_Dt,Vip','', $config['per_page'],$start,false,"array",$str_orderby);
        
        // Make action Edit/Delete
        // --------------------------------------------------
        for( $i = 0; $i < count( $results ); $i++ ){
	       $id = array_values( $results[ $i ]);
            $results[ $i ][ 'Edit'   ]   =  anchor( base_url( ) . 'index.php/categories/edit/' . $id[ 0 ], 'Edit');  
            $results[ $i ][ 'Delete' ]   =  anchor( 
                        base_url( ) . 'index.php/categories/delete/' . $id[ 0 ], 
                        'Delete',
                        array( 'onClick' => 'return deletechecked(\' ' . base_url() . 'index.php/categories/delete/' . $id[ 0 ] . ' \')' ) );                        
        }
        
        $this->data[ 'results'        ]     =   $results;
        $this->data[ 'master_limited' ]     =   explode(',', 'Ca_ID,Name,Create_Dt,Vip');
        $this->data[ 'action'         ]     =   array(array('id' => 'new','label' => 'Thêm danh mục','url' => 'index.php/categories/add/',),array('id' => 'edit','label' => 'Edit','url' => 'index.php/categories/edit/',),array('id' => 'delete','label' => 'Delete','url' => '',),);
        // $this->load->view( 'categories_list', $this->data ); 
		$this->template->write( 'title', 'Spa - Danh mục' );
        $this->template->write_view( 'content', 'categories_list.php',$this->data );
        $this->template->render();
    }
	

    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Load form add new record
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		N/A
     |								
     | - Return			: 
     |			- N/A 
    \* ------------------------------------------------------------ */
    function add( ){            
		$this->data[ 'custom_error' ] = '';
		$username = "noname";
		$userid = 0;
        
        if( $this->form_validation->run( 'categories_add' ) == false ){
             $this->data[ 'custom_error' ] = ( validation_errors( ) ? '<div class="form_error">' . validation_errors( ) . '</div>' : false );
        }else{        
            $checked = $this->input->post('Vip');
            $isVIP = 0;
            if ($checked != '') {
                $isVIP = 1;
            }
            $data = array(
                    'Name' => set_value('Name'),
					'Create_Dt' => time(),
                                        'Vip' => $isVIP,
            );
            $result = $this->CodeGenerator_model->add( 'categories', $data, 'Ca_ID' );
			if( $result == ADD_SUCCESSFULLY ){
				redirect( base_url() . 'index.php/categories/manage/' );
			}else if( $result == RECORD_EXISTED ){
				$this->data[ 'custom_error' ] = '<div class="form_error"><p>A record existed.</p></div>';
			}else{
				$this->data[ 'custom_error' ] = '<div class="form_error"><p>An Error Occured.</p></div>';
			}
		}		   
		// $this->load->view( 'categories_add', $this->data );   
		$this->template->write( 'title', 'Spa - Thêm danh mục' );
        $this->template->write_view( 'content', 'categories_add.php',$this->data );
        $this->template->render();
    }	

    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Load edit form.
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		N/A
     |								
     | - Return			: 
     |			- N/A 
    \* ------------------------------------------------------------ */
    function edit( ){
        $this->data[ 'custom_error' ] = '';
		$username = "noname";
		$userid = 0;
        
        if( $this->form_validation->run( 'categories_edit' ) == false ){
            $this->data[ 'custom_error' ] = ( validation_errors( ) ? '<div class="form_error">' . validation_errors( ) . '</div>' : false );

        }else{              
            $checked = $this->input->post('Vip');
            $isVIP = 0;
            if ($checked != '') {
                $isVIP = 1;
            }
            $data = array(
                'Name' => $this->input->post('Name'),
					'Create_Dt' => time(),
                                        'Vip' => $isVIP,
            );
			if( $this->CodeGenerator_model->edit( 'categories', $data, 'Ca_ID', $this->input->post( 'Ca_ID' ) ) == TRUE ){
				redirect( base_url( ) . 'index.php/categories/manage/' );
			}else{
				$this->data[ 'custom_error' ] = '<div class="form_error"><p>An Error Occured</p></div>';
			}
		}
		$this->data[ 'result' ]   =   $this->CodeGenerator_model->get( 'categories', 'Ca_ID,Name,Create_Dt,Vip', 'Ca_ID = "' . $this->uri->segment( 3 ) . '"', NULL, NULL, true );  
		// $this->load->view( 'categories_edit', $this->data );		
		$this->template->write( 'title', 'Spa - Thêm danh mục' );
        $this->template->write_view( 'content', 'categories_edit.php',$this->data );
        $this->template->render();
    }

    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Delete record
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		N/A
     |								
     | - Return			: 
     |			- N/A 
    \* ------------------------------------------------------------ */
    function delete( ){
        $ID =  $this->uri->segment( 3 );
        $this->CodeGenerator_model->delete(  'categories', 'Ca_ID', $ID );             
        redirect( base_url( ) . 'index.php/categories/manage/' );
    }       
}